package org.practice.validate;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

import org.junit.Test;

import junit.framework.Assert;

/**
 * 操作JDK 反射 API 
 * @author jack
 *
 */
public class TestReflect_Validation {
	@Test
	public void testReflectOfLogsBean() {
		System.err.println("链接跳转到相应文件位置: ("+LogsBean.class.getSimpleName()+".java:1)");
		
		// TODO Auto-generated method stub
		Class<?> clazz=null;
		try {
			clazz=Class.forName(LogsBean.class.getName());
			//JSGenerator jSGenerator=(JSGenerator)clazz.newInstance();//InstantiationException,IllegalAccessException
			Constructor<?> constr=clazz.getConstructor(String.class);//NoSuchMethodException
			LogsBean logsBean=(LogsBean)constr.newInstance("根据bean的Annotation生成jquery_validation用的rules JS");//InstantiationException,IllegalArgumentException,InvocationTargetException
			logsBean.rule();
			
			//Field field=clazz.getDeclaredField("message");
			Field[] fields=clazz.getDeclaredFields();
			System.out.println("length:"+fields.length);
			for(Field f:fields){
				System.out.println(f.getName());
				f.setAccessible(true);
				System.out.println(f.get(logsBean));
				System.out.println(f.getType().getSimpleName());
				
				//方法一
				/*Annotation[] annotations=f.getDeclaredAnnotations();//返回此元素上的所有注解（将忽略继承的注释），区别getAnnotations()
				for(Annotation a:annotations){
					Class<? extends Annotation> c=a.annotationType();
					System.out.println("包名加注解名===="+c.getName());
					if(a instanceof Length){
						Length len=(Length)a;
						System.out.println(len.min()+"\t"+len.max()+"\t"+len.message());
					}else if(a instanceof NotNull){
						NotNull not=(NotNull)a;
						System.out.println(not.message());
					}
				}*/
				
				if(f.isAnnotationPresent(Length.class)){
					Length len=f.getAnnotation(Length.class);//获得指定的注解
					System.out.println(len.min()+"\t"+len.max()+"\t"+len.message());
				}else if(f.isAnnotationPresent(NotNull.class)){
					NotNull not=f.getAnnotation(NotNull.class);//获得指定的注解
					System.out.println(not.message());
				}
			}
			
			//Method method=clazz.getDeclaredMethod("info", String.class);
			Method[] methods=clazz.getDeclaredMethods();
			System.out.println("methods length: "+methods.length);
			for(Method m:methods){
				System.out.println("method name: "+m.getName());
				m.setAccessible(true);
				if("info".equals(m.getName())){
					m.invoke(logsBean, "记录日志信息......");
				}
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	@Test
	public void testJSValidaion() {
		JSValidationGenerator jsGenerator=new JSValidationGenerator();
		// TODO Auto-generated method stub
		Class<?> clazz=null;
		try {
			clazz=Class.forName(LogsBean.class.getName());
			Field[] fields=clazz.getDeclaredFields();
			for(Field f:fields){
				f.setAccessible(true);
				Annotation[] annotations=f.getAnnotations();
				for(Annotation a:annotations){
					if(a instanceof Length){
						Length len=(Length)a;
						JSValidationLengthImpl jsLen=new JSValidationLengthImpl(len.min(),len.max(),len.message());
						jsGenerator.addFieldValidation(f.getName(), jsLen);
					}else if(a instanceof NotNull){
						NotNull notNull=(NotNull)a;
						JSValidationNotNullImpl jsNotNull=new JSValidationNotNullImpl(notNull.message());
						jsGenerator.addFieldValidation(f.getName(), jsNotNull);
					}
				}
			}
			
			System.out.println(jsGenerator.generate());
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
	}
	
}
